package com.jsgt.greekfire.ui.mine.user.securityset.bindmail

import android.os.CountDownTimer
import android.text.TextUtils
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseActivity
import com.fhx.core.utils.StringUtil
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.UserBean
import com.jsgt.greekfire.factory.UserViewModelFactory
import com.jsgt.greekfire.viewmodel.UserViewModel
import kotlinx.android.synthetic.main.activity_bind_mail.*
import kotlinx.android.synthetic.main.activity_bind_mail.confirm_tv
import kotlinx.android.synthetic.main.activity_bind_mail.send_code_tv

/**
 * 绑定邮箱(以前有的话就是更换)
 * author fhx
 * create time 2020/4/23 16:20
 **/
class BindMailActivity : BaseActivity() {
    private val userViewModel: UserViewModel by lazy {
        ViewModelProvider(
            this,
            UserViewModelFactory()
        ).get(UserViewModel::class.java)
    }

    override fun getLayoutId(): Int = R.layout.activity_bind_mail
    private var isChange: Boolean = false//是否是更换邮箱
    private var countDown: CountDownTimer? = null

    override fun main() {
        if (TextUtils.isEmpty(UserBean.instance.email)) {
            title = getString(R.string.bind_mail)
        } else {
            title = getString(R.string.change_mail)
            isChange = true
        }
        send_code_tv.setOnClickListener {
            val email = bind_mail_et.text.toString().trim()
            if (TextUtils.isEmpty(email)) {
                ToastUtils.show(this, "邮箱不能为空")
            } else {
                if (StringUtil.isEmail(email)) {
                    if (isChange && email == UserBean.instance.email) {
                        ToastUtils.show(this, "邮箱不能相同")
                    } else {
                        userViewModel.sendEmail(email)
                    }
                } else {
                    showToast("邮箱格式不对")
                }

            }
        }
        confirm_tv.setOnClickListener {
            checkMailCode()
        }

        userViewModel.sendEmailLiveData.observe(this, Observer {
            ToastUtils.show(this, "已发送")
            send_code_tv.isEnabled = false
            countDown = object : CountDownTimer(60000, 1000) {
                override fun onFinish() {
                    send_code_tv.text = getString(R.string.send_sms_code)
                    send_code_tv.isEnabled = true
                }

                override fun onTick(millisUntilFinished: Long) {
                    send_code_tv.text = "${millisUntilFinished / 1000}${getString(R.string.second)}"
                }
            }
            countDown?.start()
        })
        userViewModel.checkEmailLiveData.observe(this, Observer {
            val email = bind_mail_et.text.toString().trim()
            val bean = UserBean.instance
            if (isChange) {
                userViewModel.editEmail(email, bean.email!!)
            } else {
                userViewModel.bindEmail(email)
            }
        })
        userViewModel.bindEmailLiveData.observe(this, Observer {
            ToastUtils.show(this, "绑定成功")
            val email = bind_mail_et.text.toString().trim()
            UserBean.instance.email = email
            finish()
        })
        userViewModel.errorLiveData.observe(this, Observer {
            showProgress(false)
        })
    }

    private fun checkMailCode() {
        val email = bind_mail_et.text.toString().trim()
        val code = bind_mail_code_et.text.toString().trim()
        if (TextUtils.isEmpty(email)) {
            ToastUtils.show(this, "邮箱不能为空")
            return
        }
        if (!StringUtil.isEmail(email)) {
            showToast("邮箱格式不对")
            return
        }
        if (isChange && email == UserBean.instance.email) {
            ToastUtils.show(this, "邮箱不能相同")
            return
        }
        if (TextUtils.isEmpty(code)) {
            ToastUtils.show(this, "请输入验证码")
            return
        }
        showProgress(true)
        userViewModel.checkEmailCode(email, code)
    }

    override fun onDestroy() {
        super.onDestroy()
        countDown?.cancel()
        countDown = null
    }
}